{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### API\n",
    "#### 引入\n",
    "你可能已经注意到了银行分成了两个区域：顾客在前台排好队来办理业务，后台完成各种对钱的操作。在中间层，前台和后台发生了交互，双方都不能越界，交互只能发生在中间的区域。\n",
    "\n",
    "在 API 术语里面，我们管中间的这块区域叫做 **接口 Interface** ；在这里各个部分发生交互。在我们的银行中，前台和后台都同意并且规定使用 Interface 来交换信息。\n",
    "\n",
    "现在，我们假定 Bob 想要取钱。他到了柜台找到了小娜，小娜知道该怎样去取钱，她取好了之后告诉了 Bob。\n",
    "\n",
    "Bob 这时候又问：“你知道今天的股票行情吗”。\n",
    "\n",
    "小娜：？？？\n",
    "\n",
    "在这个例子里面，小娜知道怎样取钱和存钱 , 但是她不知道如何去给Bob查查今天的股票行情是怎样的。这个例子引出了另一个概念 **协议（Protocol）**。在 API 术语中，协议 是规定了各部分之间如何进行交流的一系列规则的集合。各部分之间想要完成交互必须理解并且遵守同一个协议。\n",
    "\n",
    "同时，我们注意到了交换时用的是中文。如果Bob 对小娜说「我想撳五百蚊出嚟呀唔該」，小娜不懂广东话，所以没法理解Bob说的是什么，这又引出了另一个概念 **格式（Format）**。在程序员的世界里，通用的格式包含 XML 和 JSON。\n",
    "\n",
    "最后，让我们假设银行想要扩展业务到股市，这时候就需要一位特殊的接待员来处理与股票相关的业务了，这时，小冰就登场啦。在 API 术语里面， **API 端点** 通常是指在同一个接口中提供特定功能子集的服务提供者。在这种情况下，小冰和小娜都是端点。（小冰和小娜是同一个柜台不同功能的提供者）不同的端点可以有不同的协议和不同的格式。\n",
    "\n",
    "总结起来：接口是不同组件进行交互的地方。协议是一系列规定了他们之间怎样进行交互的规则。格式定义了他们通过什么进行交流（JSON或者是XML），端点可以在同一个接口里面提供不同的功能。\n",
    "\n",
    "https://www.youtube.com/watch?v=s7wmiS2mSXY\n",
    "\n",
    "https://www.bilibili.com/video/BV137411z7KM/?spm_id_from=333.788&vd_source=07ac6468c90515cc50b6c8b1072b7271\n",
    "\n",
    "#### What is API\n",
    "\n",
    "**API**（ Application Programming Interface），它能够帮你实现轻松的和其他软件组件（如服务器，操作系统等）的交互。\n",
    "\n",
    "API stands for **Application Programming Interface**. An API is a software intermediary that allows two applications to talk to each other.  In other words, an API is the messenger that delivers your request to the provider that you’re requesting it from and then delivers the response back to you.\n",
    "\n",
    "#### How do APIs work?\n",
    "如何理解API，API 是如何工作的\n",
    "\n",
    "（1）接口（Interface）：\n",
    "    我们管中间的这块区域（银行柜台）叫做接口 Interface ；在这里各个部分发生交互。在我们的银行中，前台和后台都同意并且规定使用 Interface 来交换信息。\n",
    "    \n",
    "（2）协议（Protocol）：\n",
    "    在 API 术语中，协议 是规定了各部分之间如何进行交流的一系列规则的集合。各部分之间想要完成交互必须理解并且遵守同一个协议。\n",
    "    \n",
    "（3）格式（Format）：\n",
    "    通用的格式包含 XML 和 JSON。\n",
    "    \n",
    "（4）API端点：\n",
    "    指在同一个接口中提供特定功能子集的服务提供者。在这种情况下，小冰和小娜都是端点。（小冰和小娜是同一个柜台不同功能的提供者）不同的端点可以有不同的协议和不同的格式。\n",
    "\n",
    "总结起来：接口是不同组件进行交互的地方。协议是一系列规定了他们之间怎样进行交互的规则。格式定义了他们通过什么进行交流（JSON或者是XML），端点可以在同一个接口里面提供不同的功能。\n",
    "\n",
    "https://blog.csdn.net/cumtdeyurenjie/article/details/80211896?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522169262007716800211545420%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=169262007716800211545420&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-80211896-null-null.142^v93^insert_down28v1&utm_term=API&spm=1018.2226.3001.4187\n",
    "\n",
    "#### Example 1\n",
    "Imagine a waiter in a restaurant.  You, the customer, are sitting at the table with a menu of choices to order from, and the kitchen is the provider who will fulfill your order.\n",
    "\n",
    "You need a link to communicate your order to the kitchen and then to deliver your food back to your table. It can’t be the chef because she’s cooking in the kitchen. You need something to connect the customer who’s ordering food and the chef who prepares it.  That’s where the **waiter** — or the **API** —  enters the picture.\n",
    "\n",
    "The waiter takes your order, delivers it to the kitchen, telling the kitchen what to do. It then delivers the response, in this case, the food, back to you. Moreover, if the API is designed correctly, hopefully, your order won’t crash!\n",
    "\n",
    "#### Example 2：A real example of an API\n",
    "\n",
    "How are APIs used in the real world? Here’s a very common scenario of the API economy at work: booking a flight.\n",
    "\n",
    "When you search for flights online, you have a menu of options to choose from. You choose a departure city and date, a return city and date, cabin class, and other variables like your meal, your seat, or baggage requests.\n",
    "\n",
    "To book your flight, you need to interact with the airline’s website to access the airline’s database to see if any seats are available on those dates, and what the cost might be based on the date, flight time, route popularity, etc.\n",
    "\n",
    "You need access to that information from the airline’s database, whether you’re interacting with it from the website or an online travel service that aggregates information from multiple airlines. Alternatively, you might be accessing the information from a mobile phone. In any case, you need to get the information, and so the application must interact with the airline’s API, giving it access to the airline’s data.\n",
    "\n",
    "**APIs provide a standard way of accessing any application data, or device**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### List of API \n",
    "\n",
    "- 1、Pandas_datareader\n",
    "- 2、Tushare\n",
    "- 3、其他"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、Pandas_datareader\n",
    "\n",
    "Pandas 库提供了从财经网站获取金融数据的API接口，可作为商业交易股票数据获取的另一种方法，该在urllib3客户端库基础上实现了身份访问网站的股票。需要注意的是当前模块已经进入pandas-datareader包中。\n",
    "https://pandas-datareader.readthedocs.io/en/latest/\n",
    "\n",
    "#### Install \n",
    "pip install pandas-datareader\n",
    "\n",
    "#### Data Readers\n",
    "https://pandas-datareader.readthedocs.io/en/latest/readers/index.html\n",
    "- AlphaVantage\n",
    "- Federal Reserve Economic Data (FRED)\n",
    "- Fama-French Data (Ken French’s Data Library)\n",
    "- Bank of Canada\n",
    "- Econdb\n",
    "- Enigma\n",
    "- Eurostat\n",
    "- The Investors Exchange (IEX)\n",
    "- Moscow Exchange (MOEX)\n",
    "- NASDAQ\n",
    "- Naver Finance\n",
    "- Organisation for Economic Co-operation and Development (OECD)\n",
    "- Quandl\n",
    "- Stooq.com\n",
    "- Tiingo\n",
    "- Thrift Savings Plan (TSP)\n",
    "- World Bank\n",
    "\n",
    "You may not be able to use all datasets. \n",
    "\n",
    "Please refer to its Github Repo for more details: [link here](https://pandas-datareader.readthedocs.io/en/latest/readers/index.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Data from FRED St.Louis\n",
    "\n",
    "FRED是美联储提供的经济数据。利率、GDP、国债收益率之类的都在这里找"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>GS10</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DATE</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-06-01</th>\n",
       "      <td>3.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-01</th>\n",
       "      <td>3.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-08-01</th>\n",
       "      <td>4.17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-09-01</th>\n",
       "      <td>4.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-10-01</th>\n",
       "      <td>4.80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            GS10\n",
       "DATE            \n",
       "2023-06-01  3.75\n",
       "2023-07-01  3.90\n",
       "2023-08-01  4.17\n",
       "2023-09-01  4.38\n",
       "2023-10-01  4.80"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas_datareader as pdr\n",
    "pd = pdr.get_data_fred('GS10')\n",
    "  # This example reads 5-years of 10-year constant maturity yields on U.S. government bonds.\n",
    "  #   最近5年月度的美国10年期国债固定收益\n",
    "  #   https://zhuanlan.zhihu.com/p/341254102\n",
    "pd.tail()\n",
    "  # 与head()函数类似，默认是取dataframe中的最后五行。\n",
    "  #   （1）https://blog.csdn.net/qq_42612717/article/details/106267626\n",
    "  #   （2）http://www.cnblogs.com/xxswkl/p/10827646.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>GDP</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DATE</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-07-01</th>\n",
       "      <td>25994.639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-10-01</th>\n",
       "      <td>26408.405</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-01</th>\n",
       "      <td>26813.601</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-04-01</th>\n",
       "      <td>27063.012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07-01</th>\n",
       "      <td>27623.543</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  GDP\n",
       "DATE                 \n",
       "2022-07-01  25994.639\n",
       "2022-10-01  26408.405\n",
       "2023-01-01  26813.601\n",
       "2023-04-01  27063.012\n",
       "2023-07-01  27623.543"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas_datareader.data as web\n",
    "  # pandas-datareader包中的pandas_datareader.data.\n",
    "  #    DataReader函数可以根据输入的证券Ticker，起始日期和终止日期来返回包含所有历史日价格的数据，\n",
    "  #    其数据类型是DataFrame\n",
    "import datetime\n",
    "start = datetime.datetime(2019,11,1)\n",
    "end = datetime.datetime(2023,11,1)\n",
    "gdp = web.DataReader('GDP', 'fred', start, end)\n",
    "gdp.loc['2023-01-01']\n",
    "\n",
    "gdp2 = web.DataReader('GDP', 'fred', start='2019-11-01', end='2023-11-01')\n",
    "gdp2.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2019, 1, 1, 0, 0)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "start"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CPIAUCSL</th>\n",
       "      <th>CPILFESL</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DATE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-11-01</th>\n",
       "      <td>260.813</td>\n",
       "      <td>269.901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-01</th>\n",
       "      <td>262.035</td>\n",
       "      <td>270.267</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-01</th>\n",
       "      <td>262.650</td>\n",
       "      <td>270.383</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-02-01</th>\n",
       "      <td>263.638</td>\n",
       "      <td>270.727</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-01</th>\n",
       "      <td>264.914</td>\n",
       "      <td>271.526</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            CPIAUCSL  CPILFESL\n",
       "DATE                          \n",
       "2020-11-01   260.813   269.901\n",
       "2020-12-01   262.035   270.267\n",
       "2021-01-01   262.650   270.383\n",
       "2021-02-01   263.638   270.727\n",
       "2021-03-01   264.914   271.526"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas_datareader.data as web\n",
    "import datetime\n",
    "start = datetime.datetime(2020, 11, 1)\n",
    "end = datetime.datetime(2023, 11, 1)\n",
    "inflation = web.DataReader(['CPIAUCSL', 'CPILFESL'], 'fred', start, end)\n",
    "  # Consumer Price Index for All Urban Consumers: All Items in U.S. City Average (CPIAUCSL)\n",
    "  #   https://fred.stlouisfed.org/graph/?g=JIWz\n",
    "  \n",
    "inflation.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 更多的案例：\n",
    "https://pandas-datareader.readthedocs.io/en/latest/remote_data.html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Data from Yahoo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get data from Yahoo\n",
    "#   2021 年 11 月 1 日起，用户将无法从中国大陆使用 Yahoo 的产品与服务。\n",
    "#   http://www.cnblogs.com/li98/p/10988809.html\n",
    "import pandas_datareader.data as web\n",
    "import pandas as pd\n",
    "import datetime as dt\n",
    "start = datetime.datetime(2019, 1, 1) # or start = '1/1/2016'\n",
    "end = datetime.date.today()\n",
    "  # datetime库有获取当前日期的类，日历日期值用datetime.date表示。\n",
    "  #   比如datetime.date.today()。\n",
    "prices = web.DataReader('AAPL', 'yahoo', start, end)\n",
    "  # 苹果（AAPL）\n",
    "prices.tail()\n",
    "\n",
    "df = web.DataReader('GE', 'yahoo', start='2019-09-10', end='2019-10-09')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 有用的命令 requests\n",
    "\n",
    "The requests module allows you to send HTTP requests using Python.\n",
    "\n",
    "The HTTP request returns a `Response Object` with all the response data (content, encoding, status, etc).\n",
    "\n",
    "Detail: https://www.w3schools.com/python/module_requests.asp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<!DOCTYPE html>\n",
      "<html>\n",
      "<body>\n",
      "\n",
      "<h1>This is a Test Page</h1>\n",
      "\n",
      "</body>\n",
      "</html>\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "x = requests.get('https://w3schools.com/python/demopage.htm')\n",
    "print(x.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### JSON\n",
    "\n",
    "JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。它基于 ECMAScript (欧洲计算机协会制定的js规范)的一个子集，采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写，同时也易于机器解析和生成，并有效地提升网络传输效率。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、Tushare\n",
    "tushare.pro\n",
    "https://tushare.pro/\n",
    "\n",
    "tushare是一个免费,开源的python财经数据接口包.主要实现对股票等金融数据从数据采集、清洗加工到数据存储的过程，能够为金融分析人员提供快速、整洁、和多样的便于分析的数据，为他们在数据获取方面极大地减轻工作量，使他们更加专注于策略和模型的研究与实现上。\n",
    "\n",
    "学习链接：\n",
    "\n",
    "（1）https://zhuanlan.zhihu.com/p/495523483\n",
    "\n",
    "（2）https://blog.csdn.net/weixin_48728394/article/details/123011181\n",
    "\n",
    "tushare_token = getpass.getpass('Please input the token: c1f4f063314e224270f26a79c09ddb050e9eac55a7d73a67e7d50360') \n",
    "\n",
    "#在：后输入token即可连接成功。\n",
    "\n",
    "#请在 tushare.pro 网站注册并且告知学生身份，可以取得你的token\n",
    "\n",
    "1、通过Python SDK 调取数据https://tushare.pro/document/1?doc_id=40\n",
    "\n",
    "2、积分制：无权限调取数据。超过300积分才行。https://www.zhihu.com/question/324643496/answer/2769296677\n",
    "\n",
    "3、获得积分的方法https://tushare.pro/document/1?doc_id=13，高校老师和学生有权限"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install tushare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please input the token: c1f4f063314e224270f26a79c09ddb050e9eac55a7d73a67e7d50360········\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>shortname</th>\n",
       "      <th>province</th>\n",
       "      <th>city</th>\n",
       "      <th>address</th>\n",
       "      <th>phone</th>\n",
       "      <th>office</th>\n",
       "      <th>website</th>\n",
       "      <th>chairman</th>\n",
       "      <th>manager</th>\n",
       "      <th>reg_capital</th>\n",
       "      <th>setup_date</th>\n",
       "      <th>end_date</th>\n",
       "      <th>employees</th>\n",
       "      <th>main_business</th>\n",
       "      <th>org_code</th>\n",
       "      <th>credit_code</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>北京广能投资基金管理有限公司</td>\n",
       "      <td>广能基金</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>北京市朝阳区北四环中路27号院5号楼2712-2715A</td>\n",
       "      <td>None</td>\n",
       "      <td>北京市朝阳区北四环中路27号院5号楼2712-2715A</td>\n",
       "      <td>www.gnfund.cn</td>\n",
       "      <td>刘锡潜</td>\n",
       "      <td>杨运成</td>\n",
       "      <td>10000.0000</td>\n",
       "      <td>20111031</td>\n",
       "      <td>None</td>\n",
       "      <td>10.0</td>\n",
       "      <td>None</td>\n",
       "      <td>584419680</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>宏源证券股份有限公司</td>\n",
       "      <td>宏源证券</td>\n",
       "      <td>新疆</td>\n",
       "      <td>乌鲁木齐市</td>\n",
       "      <td>新疆维吾尔自治区乌鲁木齐市文艺路233号宏源大厦</td>\n",
       "      <td>86-991-2301870</td>\n",
       "      <td>新疆维吾尔自治区乌鲁木齐市文艺路233号宏源大厦</td>\n",
       "      <td>www.hysec.com</td>\n",
       "      <td>冯戎</td>\n",
       "      <td>冯戎</td>\n",
       "      <td>397240.8332</td>\n",
       "      <td>19930525</td>\n",
       "      <td>None</td>\n",
       "      <td>5347.0</td>\n",
       "      <td>主要业务:代理买卖证券.</td>\n",
       "      <td>228593068</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>国元证券股份有限公司</td>\n",
       "      <td>国元证券</td>\n",
       "      <td>安徽</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>安徽省合肥市梅山路18号</td>\n",
       "      <td>86-551-62207323,86-551-62207968</td>\n",
       "      <td>安徽省合肥市梅山路18号</td>\n",
       "      <td>www.gyzq.com.cn</td>\n",
       "      <td>蔡咏</td>\n",
       "      <td>俞仕新</td>\n",
       "      <td>336544.7047</td>\n",
       "      <td>19970606</td>\n",
       "      <td>None</td>\n",
       "      <td>3330.0</td>\n",
       "      <td>主营业务:经纪业务,自营投资业务,投行业务,资产管理业务,基金管理业务,期货业务,境外业务国...</td>\n",
       "      <td>731686376</td>\n",
       "      <td>91340000731686376P</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>广发证券股份有限公司</td>\n",
       "      <td>广发证券</td>\n",
       "      <td>广东</td>\n",
       "      <td>广州市</td>\n",
       "      <td>广东省广州市黄埔区中新广州知识城腾飞一街2号618室</td>\n",
       "      <td>86-20-87555888,86-20-87550565,86-20-87550265</td>\n",
       "      <td>广东省广州市天河区天河北路183-187号大都会广场40楼5楼,7楼,8楼,18楼,19楼,...</td>\n",
       "      <td>www.gf.com.cn</td>\n",
       "      <td>孙树明</td>\n",
       "      <td>林治海</td>\n",
       "      <td>762108.7664</td>\n",
       "      <td>19940121</td>\n",
       "      <td>None</td>\n",
       "      <td>12103.0</td>\n",
       "      <td>主营业务:证券经纪</td>\n",
       "      <td>126335439</td>\n",
       "      <td>91440000126335439C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>长江证券股份有限公司</td>\n",
       "      <td>长江证券</td>\n",
       "      <td>湖北</td>\n",
       "      <td>武汉市</td>\n",
       "      <td>湖北省武汉市江汉区新华路特8号</td>\n",
       "      <td>86-27-65799866,86-27-65799856</td>\n",
       "      <td>湖北省武汉市江汉区新华路特8号</td>\n",
       "      <td>www.cjsc.com</td>\n",
       "      <td>尤习贵</td>\n",
       "      <td>刘元瑞</td>\n",
       "      <td>552947.0960</td>\n",
       "      <td>19970724</td>\n",
       "      <td>None</td>\n",
       "      <td>6637.0</td>\n",
       "      <td>主营业务:证券经纪</td>\n",
       "      <td>700821272</td>\n",
       "      <td>91420000700821272A</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             name shortname province   city                       address  \\\n",
       "0  北京广能投资基金管理有限公司      广能基金       北京    北京市  北京市朝阳区北四环中路27号院5号楼2712-2715A   \n",
       "1      宏源证券股份有限公司      宏源证券       新疆  乌鲁木齐市      新疆维吾尔自治区乌鲁木齐市文艺路233号宏源大厦   \n",
       "2      国元证券股份有限公司      国元证券       安徽    合肥市                  安徽省合肥市梅山路18号   \n",
       "3      广发证券股份有限公司      广发证券       广东    广州市    广东省广州市黄埔区中新广州知识城腾飞一街2号618室   \n",
       "4      长江证券股份有限公司      长江证券       湖北    武汉市               湖北省武汉市江汉区新华路特8号   \n",
       "\n",
       "                                          phone  \\\n",
       "0                                          None   \n",
       "1                                86-991-2301870   \n",
       "2               86-551-62207323,86-551-62207968   \n",
       "3  86-20-87555888,86-20-87550565,86-20-87550265   \n",
       "4                 86-27-65799866,86-27-65799856   \n",
       "\n",
       "                                              office          website  \\\n",
       "0                       北京市朝阳区北四环中路27号院5号楼2712-2715A    www.gnfund.cn   \n",
       "1                           新疆维吾尔自治区乌鲁木齐市文艺路233号宏源大厦    www.hysec.com   \n",
       "2                                       安徽省合肥市梅山路18号  www.gyzq.com.cn   \n",
       "3  广东省广州市天河区天河北路183-187号大都会广场40楼5楼,7楼,8楼,18楼,19楼,...    www.gf.com.cn   \n",
       "4                                    湖北省武汉市江汉区新华路特8号     www.cjsc.com   \n",
       "\n",
       "  chairman manager  reg_capital setup_date end_date  employees  \\\n",
       "0      刘锡潜     杨运成   10000.0000   20111031     None       10.0   \n",
       "1       冯戎      冯戎  397240.8332   19930525     None     5347.0   \n",
       "2       蔡咏     俞仕新  336544.7047   19970606     None     3330.0   \n",
       "3      孙树明     林治海  762108.7664   19940121     None    12103.0   \n",
       "4      尤习贵     刘元瑞  552947.0960   19970724     None     6637.0   \n",
       "\n",
       "                                       main_business   org_code  \\\n",
       "0                                               None  584419680   \n",
       "1                                       主要业务:代理买卖证券.  228593068   \n",
       "2  主营业务:经纪业务,自营投资业务,投行业务,资产管理业务,基金管理业务,期货业务,境外业务国...  731686376   \n",
       "3                                          主营业务:证券经纪  126335439   \n",
       "4                                          主营业务:证券经纪  700821272   \n",
       "\n",
       "          credit_code  \n",
       "0                None  \n",
       "1                None  \n",
       "2  91340000731686376P  \n",
       "3  91440000126335439C  \n",
       "4  91420000700821272A  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tushare as ts\n",
    "import getpass\n",
    "  #  getpass模块提供了平台无关的在命令行下输入密码的方法; 该模块主要提供:\n",
    "  #      两个函数: getuser, getpass\n",
    "tushare_token = getpass.getpass('Please input the token: c1f4f063314e224270f26a79c09ddb050e9eac55a7d73a67e7d50360') \n",
    "  # 在：后输入token即可连接成功。\n",
    "  #     请在 tushare.pro 网站注册并且告知学生身份，可以取得你的token\n",
    "pro = ts.pro_api(tushare_token)\n",
    "  # 可以在初始化接口里直接设置token\n",
    "  #     直接用 pro = ts.pro_api('c1f4f063314e224270f26a79c09ddb050e9eac55a7d73a67e7d50360') 也可\n",
    "df = pro.fund_company()\n",
    "  # 接口：fund_company\n",
    "  # 描述：获取公募基金管理人列表\n",
    "  # https://tushare.pro/document/2?doc_id=118\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>chairman</th>\n",
       "      <th>manager</th>\n",
       "      <th>secretary</th>\n",
       "      <th>setup_date</th>\n",
       "      <th>province</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>301052.SZ</td>\n",
       "      <td>路金波</td>\n",
       "      <td>瞿洪斌</td>\n",
       "      <td>蔡钰如</td>\n",
       "      <td>20120606</td>\n",
       "      <td>浙江</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>301259.SZ</td>\n",
       "      <td>钟儒波</td>\n",
       "      <td>游建军</td>\n",
       "      <td>殷明坤</td>\n",
       "      <td>20130204</td>\n",
       "      <td>湖南</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>301283.SZ</td>\n",
       "      <td>陈曙光</td>\n",
       "      <td>陈曙光</td>\n",
       "      <td>廖燕桃</td>\n",
       "      <td>20121019</td>\n",
       "      <td>广东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>301117.SZ</td>\n",
       "      <td>王进</td>\n",
       "      <td>王进</td>\n",
       "      <td>张顺</td>\n",
       "      <td>19940830</td>\n",
       "      <td>四川</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>003012.SZ</td>\n",
       "      <td>何新明</td>\n",
       "      <td>何颖</td>\n",
       "      <td>黄征</td>\n",
       "      <td>20111104</td>\n",
       "      <td>广东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2947</th>\n",
       "      <td>301262.SZ</td>\n",
       "      <td>张晓刚</td>\n",
       "      <td>张晓刚</td>\n",
       "      <td>邓强</td>\n",
       "      <td>20101126</td>\n",
       "      <td>山东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2948</th>\n",
       "      <td>002776.SZ</td>\n",
       "      <td>陈亚生</td>\n",
       "      <td>陈亚生</td>\n",
       "      <td>刘志伟</td>\n",
       "      <td>20061129</td>\n",
       "      <td>广东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2949</th>\n",
       "      <td>003816.SZ</td>\n",
       "      <td>杨长利</td>\n",
       "      <td>高立刚</td>\n",
       "      <td>尹恩刚</td>\n",
       "      <td>20140325</td>\n",
       "      <td>广东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2950</th>\n",
       "      <td>300573.SZ</td>\n",
       "      <td>刘继东</td>\n",
       "      <td>高峨</td>\n",
       "      <td>张少尧</td>\n",
       "      <td>19770324</td>\n",
       "      <td>辽宁</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2951</th>\n",
       "      <td>002779.SZ</td>\n",
       "      <td>吴明根</td>\n",
       "      <td>吴明根</td>\n",
       "      <td>方路遥</td>\n",
       "      <td>19971210</td>\n",
       "      <td>浙江</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2952 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        ts_code chairman manager secretary setup_date province\n",
       "0     301052.SZ      路金波     瞿洪斌       蔡钰如   20120606       浙江\n",
       "1     301259.SZ      钟儒波     游建军       殷明坤   20130204       湖南\n",
       "2     301283.SZ      陈曙光     陈曙光       廖燕桃   20121019       广东\n",
       "3     301117.SZ       王进      王进        张顺   19940830       四川\n",
       "4     003012.SZ      何新明      何颖        黄征   20111104       广东\n",
       "...         ...      ...     ...       ...        ...      ...\n",
       "2947  301262.SZ      张晓刚     张晓刚        邓强   20101126       山东\n",
       "2948  002776.SZ      陈亚生     陈亚生       刘志伟   20061129       广东\n",
       "2949  003816.SZ      杨长利     高立刚       尹恩刚   20140325       广东\n",
       "2950  300573.SZ      刘继东      高峨       张少尧   19770324       辽宁\n",
       "2951  002779.SZ      吴明根     吴明根       方路遥   19971210       浙江\n",
       "\n",
       "[2952 rows x 6 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取上市公司基础信息\n",
    "# https://tushare.pro/document/2?doc_id=112\n",
    "# 接口：stock_company，可以通过数据工具调试和查看数据。\n",
    "# 描述：获取上市公司基础信息，单次提取4500条，可以根据交易所分批提取\n",
    "# 输入的数据：\n",
    "#     （1）exchange='SZSE'：深圳证券交易所。http://www.szse.cn/\n",
    "#     （2）fields：输出的数据\n",
    "# 输出的数据：\n",
    "#     （1）ts_code,    股票代码\n",
    "#     （2）chairman,   法人代表\n",
    "#     （3）manager,    总经理\n",
    "#     （4）secretary,  董秘\n",
    "#     （5）setup_date, 注册日期\n",
    "#     （6）province，  所在省份\n",
    "df = pro.stock_company(exchange='SZSE', fields='ts_code,chairman,manager,secretary,setup_date,province')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>publish_date</th>\n",
       "      <th>country</th>\n",
       "      <th>province</th>\n",
       "      <th>confirmed_num</th>\n",
       "      <th>update_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>6239</th>\n",
       "      <td>20200122</td>\n",
       "      <td>中国</td>\n",
       "      <td>辽宁省</td>\n",
       "      <td>0</td>\n",
       "      <td>2020-01-22 03:28:10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6240</th>\n",
       "      <td>20200122</td>\n",
       "      <td>中国</td>\n",
       "      <td>台湾</td>\n",
       "      <td>1</td>\n",
       "      <td>2020-01-22 03:28:10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6241</th>\n",
       "      <td>20200122</td>\n",
       "      <td>中国</td>\n",
       "      <td>香港</td>\n",
       "      <td>0</td>\n",
       "      <td>2020-01-22 03:28:10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6242</th>\n",
       "      <td>20200122</td>\n",
       "      <td>中国</td>\n",
       "      <td>黑龙江省</td>\n",
       "      <td>0</td>\n",
       "      <td>2020-01-22 03:28:10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6243</th>\n",
       "      <td>20200122</td>\n",
       "      <td>中国</td>\n",
       "      <td>湖南省</td>\n",
       "      <td>1</td>\n",
       "      <td>2020-01-22 03:28:10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     publish_date country province  confirmed_num          update_time\n",
       "6239     20200122      中国      辽宁省              0  2020-01-22 03:28:10\n",
       "6240     20200122      中国       台湾              1  2020-01-22 03:28:10\n",
       "6241     20200122      中国       香港              0  2020-01-22 03:28:10\n",
       "6242     20200122      中国     黑龙江省              0  2020-01-22 03:28:10\n",
       "6243     20200122      中国      湖南省              1  2020-01-22 03:28:10"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 全球新冠疫情数据\n",
    "# https://tushare.pro/document/2?doc_id=213\n",
    "# 接口：ncov_global\n",
    "# 描述：获取全球新冠疫情数据，包括国家和地区\n",
    "pro = ts.pro_api(tushare_token)\n",
    "df = pro.ncov_global(country='美国', fields='country,publish_date,confirmed_num,update_time')\n",
    "df.tail()\n",
    "\n",
    "# 注意：同一日期可能有多条数据，当日可能多次公布，可以采用update_time最新时间的数据。\n",
    "# 如果取country=‘中国'，包含了各省市的明细数据。\n",
    "# 0、中国-省市-数据\n",
    "df = pro.ncov_global(country='中国', fields='country,province,publish_date,confirmed_num,update_time')\n",
    "df.tail()\n",
    "# 1、查询各接口。\n",
    "#    https://tushare.pro/document/2?doc_id=27\n",
    "# 2、新闻数据具体接口：\n",
    "#    https://tushare.pro/document/2?doc_id=213"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>title</th>\n",
       "      <th>content</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>20231101</td>\n",
       "      <td>《求是》杂志发表习近平总书记重要文章《在二十届中央审计委员会第一次会议上的讲话》</td>\n",
       "      <td>11月1日出版的第21期《求是》杂志发表中共中央总书记、国家主席、中央军委主席习近平的重要文...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20231101</td>\n",
       "      <td>习近平《建设开放包容、互联互通、共同发展的世界——在第三届“一带一路”国际合作高峰论坛开幕式...</td>\n",
       "      <td>国家主席习近平《建设开放包容、互联互通、共同发展的世界——在第三届“一带一路”国际合作高峰论...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>20231101</td>\n",
       "      <td>为中国式现代化建设贡献巾帼智慧和力量——习近平总书记在同全国妇联新一届领导班子成员集体谈话时...</td>\n",
       "      <td>习近平总书记在同全国妇联新一届领导班子成员集体谈话时强调，要坚定不移走中国特色社会主义妇女发...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>20231101</td>\n",
       "      <td>全国政协十四届常委会第四次会议举行全体会议 围绕“加强生态环境保护，推进美丽中国建设”进行大...</td>\n",
       "      <td>政协第十四届全国委员会常务委员会第四次会议1日下午举行全体会议，13位常委围绕“加强生态环境...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>20231101</td>\n",
       "      <td>韩正会见加拿大前总理</td>\n",
       "      <td>11月1日，国家副主席韩正在北京会见加拿大前总理克雷蒂安。韩正表示，你是中国人民的老朋友、好...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       date                                              title  \\\n",
       "0  20231101           《求是》杂志发表习近平总书记重要文章《在二十届中央审计委员会第一次会议上的讲话》   \n",
       "1  20231101  习近平《建设开放包容、互联互通、共同发展的世界——在第三届“一带一路”国际合作高峰论坛开幕式...   \n",
       "2  20231101  为中国式现代化建设贡献巾帼智慧和力量——习近平总书记在同全国妇联新一届领导班子成员集体谈话时...   \n",
       "3  20231101  全国政协十四届常委会第四次会议举行全体会议 围绕“加强生态环境保护，推进美丽中国建设”进行大...   \n",
       "4  20231101                                         韩正会见加拿大前总理   \n",
       "\n",
       "                                             content  \n",
       "0  11月1日出版的第21期《求是》杂志发表中共中央总书记、国家主席、中央军委主席习近平的重要文...  \n",
       "1  国家主席习近平《建设开放包容、互联互通、共同发展的世界——在第三届“一带一路”国际合作高峰论...  \n",
       "2  习近平总书记在同全国妇联新一届领导班子成员集体谈话时强调，要坚定不移走中国特色社会主义妇女发...  \n",
       "3  政协第十四届全国委员会常务委员会第四次会议1日下午举行全体会议，13位常委围绕“加强生态环境...  \n",
       "4  11月1日，国家副主席韩正在北京会见加拿大前总理克雷蒂安。韩正表示，你是中国人民的老朋友、好...  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 新闻联播\n",
    "# https://tushare.pro/document/2?doc_id=154\n",
    "# 接口：cctv_news\n",
    "# 描述：获取新闻联播文字稿数据，数据开始于2006年6月，超过12年历史\n",
    "#     输入数据\n",
    "#    （1）date='20211105'：日期（输入格式：YYYYMMDD 比如：20181211）\n",
    "# 您每分钟最多访问该接口1次，权限的具体详情访问：https://tushare.pro/document/1?doc_id=108。\n",
    "df = pro.cctv_news(date='20231101')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>change</th>\n",
       "      <th>pct_chg</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20231101</td>\n",
       "      <td>10.55</td>\n",
       "      <td>10.56</td>\n",
       "      <td>10.45</td>\n",
       "      <td>10.48</td>\n",
       "      <td>10.46</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.1912</td>\n",
       "      <td>567300.35</td>\n",
       "      <td>595211.885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20231031</td>\n",
       "      <td>10.43</td>\n",
       "      <td>10.49</td>\n",
       "      <td>10.41</td>\n",
       "      <td>10.46</td>\n",
       "      <td>10.45</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.0957</td>\n",
       "      <td>652855.23</td>\n",
       "      <td>682212.138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20231030</td>\n",
       "      <td>10.40</td>\n",
       "      <td>10.47</td>\n",
       "      <td>10.35</td>\n",
       "      <td>10.45</td>\n",
       "      <td>10.45</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>805689.55</td>\n",
       "      <td>837646.002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20231027</td>\n",
       "      <td>10.38</td>\n",
       "      <td>10.48</td>\n",
       "      <td>10.33</td>\n",
       "      <td>10.45</td>\n",
       "      <td>10.41</td>\n",
       "      <td>0.04</td>\n",
       "      <td>0.3842</td>\n",
       "      <td>919771.36</td>\n",
       "      <td>957587.534</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20231026</td>\n",
       "      <td>10.31</td>\n",
       "      <td>10.42</td>\n",
       "      <td>10.30</td>\n",
       "      <td>10.41</td>\n",
       "      <td>10.38</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.2890</td>\n",
       "      <td>599991.47</td>\n",
       "      <td>621915.277</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code trade_date   open   high    low  close  pre_close  change  \\\n",
       "0  000001.SZ   20231101  10.55  10.56  10.45  10.48      10.46    0.02   \n",
       "1  000001.SZ   20231031  10.43  10.49  10.41  10.46      10.45    0.01   \n",
       "2  000001.SZ   20231030  10.40  10.47  10.35  10.45      10.45    0.00   \n",
       "3  000001.SZ   20231027  10.38  10.48  10.33  10.45      10.41    0.04   \n",
       "4  000001.SZ   20231026  10.31  10.42  10.30  10.41      10.38    0.03   \n",
       "\n",
       "   pct_chg        vol      amount  \n",
       "0   0.1912  567300.35  595211.885  \n",
       "1   0.0957  652855.23  682212.138  \n",
       "2   0.0000  805689.55  837646.002  \n",
       "3   0.3842  919771.36  957587.534  \n",
       "4   0.2890  599991.47  621915.277  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A股日线行情\n",
    "# https://tushare.pro/document/2?doc_id=27\n",
    "# 接口：daily，可以通过数据工具调试和查看数据\n",
    "# 数据说明：交易日每天15点～16点之间入库。本接口是未复权行情，停牌期间不提供数据\n",
    "# 描述：获取股票行情数据，或通过通用行情接口获取数据，包含了前后复权数据\n",
    "# 输入的数据：\n",
    "#    （1）ts_code='000001.SZ'：股票代码（支持多个股票同时提取，逗号分隔）\n",
    "#    （2）start_date、end_date：开始日期(YYYYMMDD)、结束日期(YYYYMMDD)\n",
    "df = pro.daily(ts_code='000001.SZ', start_date='20201101', end_date='20231101')\n",
    "  # 平安银行(000001)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index_code</th>\n",
       "      <th>industry_name</th>\n",
       "      <th>level</th>\n",
       "      <th>industry_code</th>\n",
       "      <th>is_pub</th>\n",
       "      <th>parent_code</th>\n",
       "      <th>src</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>801010.SI</td>\n",
       "      <td>农林牧渔</td>\n",
       "      <td>L1</td>\n",
       "      <td>110000</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>SW2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>801030.SI</td>\n",
       "      <td>基础化工</td>\n",
       "      <td>L1</td>\n",
       "      <td>220000</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>SW2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>801040.SI</td>\n",
       "      <td>钢铁</td>\n",
       "      <td>L1</td>\n",
       "      <td>230000</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>SW2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>801050.SI</td>\n",
       "      <td>有色金属</td>\n",
       "      <td>L1</td>\n",
       "      <td>240000</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>SW2021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>801080.SI</td>\n",
       "      <td>电子</td>\n",
       "      <td>L1</td>\n",
       "      <td>270000</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>SW2021</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  index_code industry_name level industry_code is_pub parent_code     src\n",
       "0  801010.SI          农林牧渔    L1        110000      1           0  SW2021\n",
       "1  801030.SI          基础化工    L1        220000      1           0  SW2021\n",
       "2  801040.SI            钢铁    L1        230000      1           0  SW2021\n",
       "3  801050.SI          有色金属    L1        240000      1           0  SW2021\n",
       "4  801080.SI            电子    L1        270000      1           0  SW2021"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取申万一级行业列表\n",
    "# 申万行业分类\n",
    "# https://tushare.pro/document/2?doc_id=181\n",
    "# 接口：index_classify\n",
    "# 描述：获取申万行业分类，可以获取申万2014年版本（28个一级分类，104个二级分类，227个三级分类）\n",
    "#      和2021年本版（31个一级分类，134个二级分类，346个三级分类）列表信息\n",
    "# 输入的参数：\n",
    "#    （1）index_code：指数代码\n",
    "#    （2）level ：行业分级（L1/L2/L3）\n",
    "#    （3）parent_code：父级代码（一级为0）\n",
    "#    （4）src：指数来源（SW2014：申万2014年版本，SW2021：申万2021年版本）\n",
    "# 输出的参数：\n",
    "#    （1）index_code ：指数代码\n",
    "#    （2）industry_name：行业名称\n",
    "#    （3）parent_code：父级代码\n",
    "#    （4）level：行业名称\n",
    "#    （5）industry_code ：行业代码\n",
    "#    （6）is_pub：是否发布了指数\n",
    "#    （7）src ：行业分类（SW申万）\n",
    "df = pro.index_classify(level='L1', src='SW2021')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>symbol</th>\n",
       "      <th>name</th>\n",
       "      <th>area</th>\n",
       "      <th>industry</th>\n",
       "      <th>list_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>000001</td>\n",
       "      <td>平安银行</td>\n",
       "      <td>深圳</td>\n",
       "      <td>银行</td>\n",
       "      <td>19910403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000002.SZ</td>\n",
       "      <td>000002</td>\n",
       "      <td>万科A</td>\n",
       "      <td>深圳</td>\n",
       "      <td>全国地产</td>\n",
       "      <td>19910129</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>000004</td>\n",
       "      <td>国华网安</td>\n",
       "      <td>深圳</td>\n",
       "      <td>软件服务</td>\n",
       "      <td>19910114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000005.SZ</td>\n",
       "      <td>000005</td>\n",
       "      <td>ST星源</td>\n",
       "      <td>深圳</td>\n",
       "      <td>环境保护</td>\n",
       "      <td>19901210</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000006.SZ</td>\n",
       "      <td>000006</td>\n",
       "      <td>深振业A</td>\n",
       "      <td>深圳</td>\n",
       "      <td>区域地产</td>\n",
       "      <td>19920427</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code  symbol  name area industry list_date\n",
       "0  000001.SZ  000001  平安银行   深圳       银行  19910403\n",
       "1  000002.SZ  000002   万科A   深圳     全国地产  19910129\n",
       "2  000004.SZ  000004  国华网安   深圳     软件服务  19910114\n",
       "3  000005.SZ  000005  ST星源   深圳     环境保护  19901210\n",
       "4  000006.SZ  000006  深振业A   深圳     区域地产  19920427"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 基础信息\n",
    "# https://tushare.pro/document/2?doc_id=25\n",
    "# 接口：stock_basic，可以通过数据工具调试和查看数据\n",
    "# 描述：获取基础信息数据，包括股票代码、名称、上市日期、退市日期等\n",
    "\n",
    "data = pro.query('stock_basic', exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')\n",
    "  # 数据调取\n",
    "  # 输入的参数：\n",
    "  #   （1）exchange：交易所。SSE上交所 SZSE深交所 BSE北交所\n",
    "  #   （2）list_status ：上市状态。L上市 D退市 P暂停上市，默认是L\n",
    "  # 输出的参数：\n",
    "  #    fields='ts_code,symbol,name,area,industry,list_date')\n",
    "  #   （1）ts_code ：TS代码\n",
    "  #   （2）symbol：股票代码\n",
    "  #   （3）name ：股票名称\n",
    "  #   （4）area ：地域\n",
    "  #   （5）industry ：所属行业\n",
    "  #   （6）list_date：上市日期\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、其他股票数据API接口\n",
    "https://blog.csdn.net/u012940698/article/details/126737630"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Homework\n",
    "\n",
    "1. 找出北交所上市公司信息并且按照“上市日期”从最近上市的开始排序（显示前10位）。\n",
    "\n",
    "2. 查询2023年11月1日的股票数据，包含市盈率、市净率、市销率、总市值（显示前10位）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please input the token: c1f4f063314e224270f26a79c09ddb050e9eac55a7d73a67e7d50360········\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>symbol</th>\n",
       "      <th>name</th>\n",
       "      <th>area</th>\n",
       "      <th>industry</th>\n",
       "      <th>list_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>227</th>\n",
       "      <td>873833.BJ</td>\n",
       "      <td>873833</td>\n",
       "      <td>美心翼申</td>\n",
       "      <td>重庆</td>\n",
       "      <td>汽车配件</td>\n",
       "      <td>20231108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>73</th>\n",
       "      <td>833030.BJ</td>\n",
       "      <td>833030</td>\n",
       "      <td>立方控股</td>\n",
       "      <td>浙江</td>\n",
       "      <td>软件服务</td>\n",
       "      <td>20231102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>176</th>\n",
       "      <td>839493.BJ</td>\n",
       "      <td>839493</td>\n",
       "      <td>并行科技</td>\n",
       "      <td>北京</td>\n",
       "      <td>软件服务</td>\n",
       "      <td>20231101</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>225</th>\n",
       "      <td>873693.BJ</td>\n",
       "      <td>873693</td>\n",
       "      <td>阿为特</td>\n",
       "      <td>上海</td>\n",
       "      <td>机械基件</td>\n",
       "      <td>20231027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>226</th>\n",
       "      <td>873726.BJ</td>\n",
       "      <td>873726</td>\n",
       "      <td>卓兆点胶</td>\n",
       "      <td>江苏</td>\n",
       "      <td>机械基件</td>\n",
       "      <td>20231019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67</th>\n",
       "      <td>832786.BJ</td>\n",
       "      <td>832786</td>\n",
       "      <td>骑士乳业</td>\n",
       "      <td>内蒙</td>\n",
       "      <td>乳制品</td>\n",
       "      <td>20231013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71</th>\n",
       "      <td>832978.BJ</td>\n",
       "      <td>832978</td>\n",
       "      <td>开特股份</td>\n",
       "      <td>湖北</td>\n",
       "      <td>汽车配件</td>\n",
       "      <td>20230928</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>224</th>\n",
       "      <td>873665.BJ</td>\n",
       "      <td>873665</td>\n",
       "      <td>科强股份</td>\n",
       "      <td>江苏</td>\n",
       "      <td>橡胶</td>\n",
       "      <td>20230926</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>60</th>\n",
       "      <td>832469.BJ</td>\n",
       "      <td>832469</td>\n",
       "      <td>富恒新材</td>\n",
       "      <td>深圳</td>\n",
       "      <td>塑料</td>\n",
       "      <td>20230918</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>135</th>\n",
       "      <td>836419.BJ</td>\n",
       "      <td>836419</td>\n",
       "      <td>万德股份</td>\n",
       "      <td>陕西</td>\n",
       "      <td>化工原料</td>\n",
       "      <td>20230915</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       ts_code  symbol  name area industry list_date\n",
       "227  873833.BJ  873833  美心翼申   重庆     汽车配件  20231108\n",
       "73   833030.BJ  833030  立方控股   浙江     软件服务  20231102\n",
       "176  839493.BJ  839493  并行科技   北京     软件服务  20231101\n",
       "225  873693.BJ  873693   阿为特   上海     机械基件  20231027\n",
       "226  873726.BJ  873726  卓兆点胶   江苏     机械基件  20231019\n",
       "67   832786.BJ  832786  骑士乳业   内蒙      乳制品  20231013\n",
       "71   832978.BJ  832978  开特股份   湖北     汽车配件  20230928\n",
       "224  873665.BJ  873665  科强股份   江苏       橡胶  20230926\n",
       "60   832469.BJ  832469  富恒新材   深圳       塑料  20230918\n",
       "135  836419.BJ  836419  万德股份   陕西     化工原料  20230915"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1、找出北交所上市公司信息并且按照“上市日期”从最近上市的开始排序（显示前10位）。\n",
    "import tushare as ts\n",
    "import getpass\n",
    "tushare_token = getpass.getpass('Please input the token: c1f4f063314e224270f26a79c09ddb050e9eac55a7d73a67e7d50360') \n",
    "pro = ts.pro_api(tushare_token)\n",
    "\n",
    "df = pro.stock_basic(exchange='BSE', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')\n",
    "df.head()\n",
    "\n",
    "sort_df = df.sort_values('list_date', ascending = False)\n",
    "sort_df.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>pe</th>\n",
       "      <th>pb</th>\n",
       "      <th>ps</th>\n",
       "      <th>total_mv</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>600186.SH</td>\n",
       "      <td>20231101</td>\n",
       "      <td>277.8250</td>\n",
       "      <td>8.5705</td>\n",
       "      <td>7.5848</td>\n",
       "      <td>1.282639e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>300737.SZ</td>\n",
       "      <td>20231101</td>\n",
       "      <td>44.5319</td>\n",
       "      <td>1.4253</td>\n",
       "      <td>1.0358</td>\n",
       "      <td>7.934911e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>600708.SH</td>\n",
       "      <td>20231101</td>\n",
       "      <td>96.6148</td>\n",
       "      <td>0.4585</td>\n",
       "      <td>0.2991</td>\n",
       "      <td>4.947574e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>003012.SZ</td>\n",
       "      <td>20231101</td>\n",
       "      <td>52.0275</td>\n",
       "      <td>1.3646</td>\n",
       "      <td>1.5166</td>\n",
       "      <td>1.051008e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>002941.SZ</td>\n",
       "      <td>20231101</td>\n",
       "      <td>29.6871</td>\n",
       "      <td>3.3953</td>\n",
       "      <td>1.3204</td>\n",
       "      <td>1.043740e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>000063.SZ</td>\n",
       "      <td>20231101</td>\n",
       "      <td>15.4088</td>\n",
       "      <td>1.8787</td>\n",
       "      <td>1.0126</td>\n",
       "      <td>1.245080e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>600918.SH</td>\n",
       "      <td>20231101</td>\n",
       "      <td>85.1290</td>\n",
       "      <td>1.3590</td>\n",
       "      <td>5.3883</td>\n",
       "      <td>5.024379e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>600520.SH</td>\n",
       "      <td>20231101</td>\n",
       "      <td>149.2868</td>\n",
       "      <td>11.3763</td>\n",
       "      <td>8.8216</td>\n",
       "      <td>3.921142e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>600483.SH</td>\n",
       "      <td>20231101</td>\n",
       "      <td>8.5443</td>\n",
       "      <td>1.0711</td>\n",
       "      <td>1.5475</td>\n",
       "      <td>2.215703e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>688480.SH</td>\n",
       "      <td>20231101</td>\n",
       "      <td>47.1086</td>\n",
       "      <td>3.4961</td>\n",
       "      <td>5.6911</td>\n",
       "      <td>3.119797e+05</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code trade_date        pe       pb      ps      total_mv\n",
       "0  600186.SH   20231101  277.8250   8.5705  7.5848  1.282639e+06\n",
       "1  300737.SZ   20231101   44.5319   1.4253  1.0358  7.934911e+05\n",
       "2  600708.SH   20231101   96.6148   0.4585  0.2991  4.947574e+05\n",
       "3  003012.SZ   20231101   52.0275   1.3646  1.5166  1.051008e+06\n",
       "4  002941.SZ   20231101   29.6871   3.3953  1.3204  1.043740e+06\n",
       "5  000063.SZ   20231101   15.4088   1.8787  1.0126  1.245080e+07\n",
       "6  600918.SH   20231101   85.1290   1.3590  5.3883  5.024379e+06\n",
       "7  600520.SH   20231101  149.2868  11.3763  8.8216  3.921142e+05\n",
       "8  600483.SH   20231101    8.5443   1.0711  1.5475  2.215703e+06\n",
       "9  688480.SH   20231101   47.1086   3.4961  5.6911  3.119797e+05"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2、查询2023年11月1日的股票数据，包含市盈率、市净率、市销率、总市值（显示前10位）。\n",
    "\n",
    "df = pro.query('daily_basic', ts_code='', trade_date='20231101',fields='ts_code,trade_date,pe,pb,ps,total_mv')\n",
    "df.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### OECD（拓展阅读）\n",
    "\n",
    "https://stats.oecd.org/\n",
    "\n",
    "The OECD has application programming interfaces (APIs) that provide access to datasets in the catalogue of OECD databases.\n",
    "\n",
    "Please see the instruction of API as follows\n",
    "\n",
    "https://data.oecd.org/api/sdmx-json-documentation/\n",
    "\n",
    "你是否可以取到其中的数据"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
